<template>
  <div class="login-container">
    <div class="login-left">
      <div class="brand-content">
        <h1 class="brand-title">电商商城</h1>
        <p class="brand-desc">您的一站式购物平台</p>
        <div class="brand-features">
          <div class="feature-item">
            <el-icon size="32px"><ShoppingCart /></el-icon>
            <span>便捷购物</span>
          </div>
          <div class="feature-item">
            <el-icon size="32px"><Van /></el-icon>
            <span>快速配送</span>
          </div>
          <div class="feature-item">
            <el-icon size="32px"><Lock /></el-icon>
            <span>安全保障</span>
          </div>
        </div>
      </div>
    </div>
    <div class="login-right">
      <div class="login-form-container">
        <h2 class="title">欢迎登录</h2>
        <el-form
          ref="formRef"
          :model="loginForm"
          :rules="rules"
          label-position="top"
          size="large"
          class="login-form"
        >
          <el-form-item prop="username" label="用户名">
            <el-input
              v-model="loginForm.username"
              :prefix-icon="User"
              placeholder="请输入用户名"
            />
          </el-form-item>
          <el-form-item prop="password" label="密码">
            <el-input
              v-model="loginForm.password"
              type="password"
              :prefix-icon="Lock"
              placeholder="请输入密码"
              show-password
            />
          </el-form-item>
          <el-form-item prop="userType" label="登录类型">
            <el-radio-group v-model="loginForm.userType" class="radio-group">
              <el-radio-button label="user">普通用户</el-radio-button>
              <el-radio-button label="admin">管理员</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <div class="remember-forgot">
            <el-checkbox v-model="loginForm.remember" size="large">记住我</el-checkbox>
            <el-button link type="primary">忘记密码？</el-button>
          </div>
          <el-form-item>
            <el-button
              type="primary"
              class="login-button"
              :loading="loading"
              @click="handleLogin"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
        <div class="register-link">
          还没有账号？
          <el-button link type="primary">立即注册</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, ShoppingCart, Van } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()
const formRef = ref()
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: '',
  userType: 'user',
  remember: false
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, message: '用户名长度不能小于3位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  userType: [
    { required: true, message: '请选择登录类型', trigger: 'change' }
  ]
}

const handleLogin = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid: boolean) => {
    if (valid) {
      loading.value = true
      try {
        await userStore.login({
          username: loginForm.username,
          password: loginForm.password,
          userType: loginForm.userType
        })
        ElMessage.success('登录成功')
        router.push(userStore.isAdmin ? '/admin' : '/mall')
      } catch (error: any) {
        ElMessage.error(error.message || '登录失败')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  width: 100%;
  display: flex;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: white;
}

.brand-content {
  text-align: center;
}

.brand-title {
  font-size: 48px;
  font-weight: bold;
  margin-bottom: 20px;
  letter-spacing: 2px;
}

.brand-desc {
  font-size: 24px;
  opacity: 0.9;
  margin-bottom: 40px;
}

.brand-features {
  display: flex;
  justify-content: center;
  gap: 40px;
  margin-top: 40px;
}

.feature-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.feature-item :deep(.el-icon) {
  color: white;
  opacity: 0.9;
}

.feature-item span {
  font-size: 16px;
  opacity: 0.8;
}

.login-right {
  flex: 1;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.login-form-container {
  width: 100%;
  max-width: 420px;
}

.title {
  text-align: center;
  color: #333;
  font-size: 32px;
  margin-bottom: 40px;
  font-weight: 600;
}

.login-form {
  width: 100%;
}

.radio-group {
  width: 100%;
  display: flex;
  gap: 20px;
}

.radio-group :deep(.el-radio-button) {
  flex: 1;
}

.radio-group :deep(.el-radio-button__inner) {
  width: 100%;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.login-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
  border-radius: 8px;
  height: 44px;
}

.register-link {
  text-align: center;
  margin-top: 24px;
  color: #666;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  padding: 8px 12px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #333;
  padding-bottom: 8px;
}

@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }

  .login-left {
    padding: 30px 20px;
  }

  .brand-title {
    font-size: 36px;
    margin-bottom: 15px;
  }

  .brand-desc {
    font-size: 20px;
    margin-bottom: 30px;
  }

  .brand-features {
    gap: 20px;
  }

  .feature-item :deep(.el-icon) {
    font-size: 24px;
  }

  .feature-item span {
    font-size: 14px;
  }

  .login-right {
    padding: 30px 20px;
  }

  .login-form-container {
    padding: 0;
  }

  .title {
    font-size: 28px;
    margin-bottom: 30px;
  }

  .radio-group {
    gap: 10px;
  }
}
</style>